<template>
  <CustomDialog :drag-dialog="dialogInfo">
    <!--表格渲染-->
    <DataGridTable
      v-loading="loading"
      :sup_this="sup_this"
      :data="data.rtnList"
      :multi-select="false"
      :show-row-number="true"
      :show-page="false"
      table-ref="reportSelectTable">
      <el-table-column prop="id" width="90px">
        <template slot="header" slot-scope="scope">
          <el-checkbox v-model="checkAll" @change="handleCheckAllChange" />
        </template>
        <template slot-scope="scope">
          <el-button v-if="selectedRows[scope.row.id]" type="danger" size="mini" @click="reportCheckSelectChange(scope.row)">取消</el-button>
          <el-button v-else type="primary" size="mini" @click="reportCheckSelectChange(scope.row)">选择</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="reportCode" label="报表编号" />
      <el-table-column prop="reportName" label="报表名称">
        <template slot="header" slot-scope="scope">
          <el-input v-model="reportName" clearable placeholder="输入回车过滤" style="width: 100%;" class="filter-item" @keyup.enter.native="filterQuery"/>
        </template>
      </el-table-column>
      <el-table-column prop="reportMenuName" label="菜单名"/>
      <el-table-column :show-overflow-tooltip="true" prop="lastUpdateDate" label="更新日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdateDate) }}</span>
        </template>
      </el-table-column>
    </DataGridTable>
    <div slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </CustomDialog>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { findReportByCpyCodeUrl } from '@/api/report/reportDefine'
import { parseTime } from '@/utils/index'

export default {
  mixins: [initData],
  props: {
    multi_select: {
      default: true,
      requried: false,
      type: Boolean
    }
  },
  data() {
    return {
      loading: false,
      selectedRows: [],
      selectedRow: {},
      sup_this: this,
      allList: [],
      checkAll: false,
      reportName: '',
      dialogInfo: {
        name: 'reportSelectDialog',
        title: '报表选择',
        fullScreen: false,
        width: 800,
        height: 80
      }
    }
  },
  created() {
    this.url = findReportByCpyCodeUrl()
    this.pageCount = false
  },
  methods: {
    parseTime,
    checkPermission,
    async beforeInit() {
      this.selectedRows = []
      this.checkAll = false
      this.selectedRow = {}
      return true
    },
    afterInit(res) {
      this.allList = res.data.rtnList
    },
    loadData() {
      this.displayDialog()
      this.init()
    },
    displayDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.show(name)
      })
    },
    hiddenDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.hide(name)
      })
    },
    filterQuery() {
      const reportName = this.reportName
      if (reportName) {
        this.data.rtnList = this.allList.filter(row => {
          return row.reportName.includes(reportName)
        })
      } else {
        this.data.rtnList = this.allList
      }
    },
    handleCurrentChange(val) {
      this.selectedRow = val
    },
    doSubmit() {
      this.loading = true
      if (this.multi_select) {
        const _selectRows = this.selectedRows
        const tmpKeys = Object.keys(_selectRows)
        const tmpRows = []
        for (const tmpKey of tmpKeys) {
          tmpRows.push(_selectRows[tmpKey])
        }
        this.$emit('reportSelectCallback', tmpRows, this)
      } else {
        this.$emit('reportSelectCallback', this.selectedRow, this)
      }
    },
    cancel() {
      this.hiddenDialog()
      this.allList = []
    },
    handleCheckAllChange(isChecked) {
      const tmpSelectRows = this.selectedRows
      if (isChecked) {
        this.data.rtnList.forEach(row => {
          tmpSelectRows[row.id] = row
        })
      } else {
        // 取消
        this.data.rtnList.forEach(row => {
          delete tmpSelectRows[row.id]
        })
      }
      this.selectedRows.splice(this.selectedRows.length)
    },
    reportCheckSelectChange(roleRow) {
      if (!this.selectedRows[roleRow.id]) {
        // 选择
        this.selectedRows[roleRow.id] = roleRow
      } else {
        delete this.selectedRows[roleRow.id]
      }
      this.selectedRows.splice(this.selectedRows.length)
    }
  }
}
</script>
